
/*
 * Hlpay-Plus aggregate payment system. Copyright
 * (c) 2022-2023 Hlpay Team Copyright has the right of final interpretation.
 */

package com.hlkj.pay.app.adminuser.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.LocalContext;
import com.hlkj.framework.common.pojo.LocalRequest;
import com.hlkj.framework.common.provider.Aes256EncryptionProvider;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.adminuser.IAdminUserAppService;
import com.hlkj.pay.app.common.IAuthCodeService;
import com.hlkj.pay.app.common.ICommonAppService;
import com.hlkj.pay.app.transfer.UserDoToResp;
import com.hlkj.pay.common.AdminUserResultCode;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.config.SmsTemplateProperties;
import com.hlkj.pay.dto.LocalAdminUserRequest;
import com.hlkj.pay.dto.admin.AdminUserDto;
import com.hlkj.pay.dto.admin.RoleQueryDto;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.admin.AdminUserDO;
import com.hlkj.pay.infrastructure.model.admin.RoleDO;
import com.hlkj.pay.service.admin.IAdminUserService;
import com.hlkj.pay.service.admin.IRoleService;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.util.UserUtil;
import com.hlkj.pay.vo.admin.req.user.*;
import com.hlkj.pay.vo.admin.resp.user.AdminUserDetailResp;
import com.hlkj.pay.vo.admin.resp.user.AdminUserResp;
import com.hlkj.pay.vo.admin.resp.user.AdminUserRoleResp;
import com.hlkj.pay.vo.common.req.SmsSendReq;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.StringPool;

/**
 * @author HlpayTeam
 * @date 2024/09/10 14:50
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminUserAppServiceImpl implements IAdminUserAppService {

    private final IAdminUserService userService;

    private final IRoleService roleService;

    private final IBusinessLogService businessLogService;

    private final Aes256EncryptionProvider aes256EncryptionProvider;

    private final ICommonAppService commonAppService;

    private final IAuthCodeService authCodeService;

    private final SmsTemplateProperties smsTemplateProperties;

    private final IMerchantService merchantService;

    @Override
    public CommonResult<AdminUserDetailResp> userDetail(Long userId) {
        try {
            if (userId == null) {
                LocalRequest localUserRequest = LocalContext.get();
                userId = localUserRequest.getUserId();
            }
            AdminUserDto adminUserDto = userService.queryUserDto(userId);
            if (adminUserDto == null) {
                return CommonResult.success(null);
            }
            adminUserDto.setEmail(adminUserDto.getEmailEncrypt());
            adminUserDto.setPhone(adminUserDto.getPhoneEncrypt());
            AdminUserDetailResp user = new AdminUserDetailResp();
            BeanUtils.copyProperties(adminUserDto, user);
            RoleQueryDto roleQueryDto = new RoleQueryDto();
            // 需要显示哪些角色给到当前用户 因此需要获取所有的角色
            List<RoleDO> roleList = roleService.queryRoleList(roleQueryDto);
            List<String> roleCodes = adminUserDto.getRoleCodes();
            roleList.stream().forEach(role -> {
                boolean check = roleCodes.contains(role.getCode());
                int checkValue = check ? CommonEnum.YES_NO_TYPE.YES.getCode() : CommonEnum.YES_NO_TYPE.NO.getCode();
                user.getUserRoleList().add(AdminUserRoleResp.builder().roleCode(role.getCode()).roleName(role.getName()).roleFlag(checkValue).build());
            });
            List<Long> userIds = new ArrayList<>();
            if (adminUserDto.getCreatedBy() != null) {
                userIds.add(adminUserDto.getCreatedBy().longValue());
            }
            if (adminUserDto.getUpdatedBy() != null) {
                userIds.add(adminUserDto.getUpdatedBy().longValue());
            }
            userIds.remove(null);
            if (CollectionUtils.isEmpty(userIds)) {
                return CommonResult.success(user);
            }
            // AdminUserQueryDto userQueryDO = new AdminUserQueryDto();
            // userQueryDO.setUserIds(userIds);
            //
            // List<AdminUserDO> userList = userService.queryUserList(userQueryDO);
            // if (CollectionUtils.isEmpty(userList)) {
            // return CommonResult.success(user);
            // }
            // Map<String, AdminUserDO> userMap = userList.stream()
            // .collect(Collectors.toMap(userDO -> userDO.getId().toString(),
            // Function.identity(), (t1, t2) -> t2));

            // AdminUserDO createdUserDO = userMap.get(String.valueOf(user.getCreatedBy()));
            // AdminUserDO updatedUserDO = userMap.get(String.valueOf(user.getUpdatedBy()));
            // if (createdUserDO != null) {
            // user.setCreatedByName(createdUserDO.getName());
            // }
            // if (updatedUserDO != null) {
            // user.setUpdatedByName(updatedUserDO.getName());
            // }
            try {
                user.setPhone(aes256EncryptionProvider.decrypt(adminUserDto.getPhoneEncrypt()));
            }
            catch (Exception e) {
                user.setPhone(adminUserDto.getPhoneEncrypt());
            }
            try {
                user.setEmail(aes256EncryptionProvider.decrypt(adminUserDto.getEmailEncrypt()));
            }
            catch (Exception e) {
                user.setEmail(adminUserDto.getEmailEncrypt());
            }
            // try {
            // user.setPwd(aes256EncryptionProvider.decrypt(adminUserDto.getPwdEncrypt()));
            // } catch (Exception e) {
            // user.setEmail(adminUserDto.getEmailEncrypt());
            // }
            return CommonResult.success(user);
        }
        catch (Exception e) {
            log.error("userDetail error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Long> addUser(AdminUserAddReq adminUserAddReq) {
        log.info("addUser adminUserAddReq:{}", JsonUtils.toJsonString(adminUserAddReq));
        try {
            if (!StringUtils.hasText(adminUserAddReq.getEmail()) && !StringUtils.hasText(adminUserAddReq.getPhone())) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
            }
            AdminUserDO user;
            if (StringUtils.hasText(adminUserAddReq.getEmail())) {
                user = userService.queryUserByEmail(adminUserAddReq.getEmail(), adminUserAddReq.getSysType());
                if (user != null) {
                    return CommonResult.error(AdminUserResultCode.USER_DOES_EXIST);
                }
            }
            if (StringUtils.hasText(adminUserAddReq.getPhone())) {
                user = userService.queryUserByPhone(adminUserAddReq.getPhone(), adminUserAddReq.getSysType());
                if (user != null) {
                    return CommonResult.error(AdminUserResultCode.USER_DOES_EXIST);
                }
            }
            if (!StringUtils.hasText(adminUserAddReq.getUserName())) {
                adminUserAddReq.setUserName(adminUserAddReq.getEmail());
            }
            AdminUserDO adminUserDO = new AdminUserDO();
            if (StringUtils.hasText(adminUserAddReq.getPwd()) && StringUtils.hasText(adminUserAddReq.getPwdConfirm())) {
                if (!adminUserAddReq.getPwd().equals(adminUserAddReq.getPwdConfirm())) {
                    return CommonResult.error(AdminUserResultCode.SECOND_PASSWORD_IS_INCONSISTENT);
                }
                adminUserDO.setPwdEncrypt(aes256EncryptionProvider.encryptString(adminUserAddReq.getPwd()));
            }
            if (!CollectionUtils.isEmpty(adminUserAddReq.getPartCodes())) {
                adminUserDO.setPartCode(JsonUtils.toJsonString(adminUserAddReq.getPartCodes()));
            }
            BeanUtils.copyProperties(adminUserAddReq, adminUserDO);
            adminUserDO.setEmailEncrypt(aes256EncryptionProvider.encryptString(adminUserAddReq.getEmail()));
            adminUserDO.setPhoneEncrypt(aes256EncryptionProvider.encryptString(adminUserAddReq.getPhone()));
            if (adminUserAddReq.getStatus() == null) {
                adminUserAddReq.setStatus(AdminUserEnum.USER_STATUS.ABLE.getCode());
            }
            adminUserDO.setStatus(adminUserAddReq.getStatus());
            // String password = defaultPwd;
            // adminUserDO.setPwdEncrypt(password);
            try {
                // if (StringUtils.hasText(adminUserDO.getEmailEncrypt())) {
                // EmailSendReq emailSendReq = new EmailSendReq();
                // emailSendReq.setBusinessCode(initBusinessCode);
                // emailSendReq.setVerifCode(password);
                // emailSendReq.setEmail(adminUserDO.getEmailEncrypt());
                // emailSendReq.setLanguage(adminUserDO.getLanguage());
                // messageSendClient.sendEmail(emailSendReq);
                // } else if (StringUtils.hasText(adminUserDO.getPhoneEncrypt())) {
                // SmsSendReq smsSendReq = new SmsSendReq();
                // smsSendReq.setBusinessCode(initBusinessCode);
                // smsSendReq.setVerifCode(password);
                // smsSendReq.setPhone(adminUserDO.getPhoneEncrypt());
                // smsSendReq.setLanguage(adminUserDO.getLanguage());
                // messageSendClient.sendSms(smsSendReq);
                // }

            }
            catch (Exception e) {
                log.error("user pwd init send message fail:{}", e.getMessage(), e);
            }
            LocalRequest localUserRequest = LocalContext.get();
            adminUserDO.setSn(localUserRequest.getSn());
            adminUserDO.setSysType(localUserRequest.getSysType());
            Long userId = userService.addUser(adminUserDO);
            return CommonResult.success(userId);
        }
        catch (Exception e) {
            log.error("addUser error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateUser(AdminUserUpdateReq adminUserUpdateReq) {
        log.info("updateUser adminUserUpdateReq:{}", JsonUtils.toJsonString(adminUserUpdateReq));
        try {
            if (adminUserUpdateReq.getUserId() == null) {
                adminUserUpdateReq.setUserId(LocalContext.get().getUserId());
            }
            AdminUserDto beforeRole = userService.queryUserDto(adminUserUpdateReq.getUserId());
            if (beforeRole == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            if (StringUtils.hasText(adminUserUpdateReq.getPhone())) {
                adminUserUpdateReq.setPhone(aes256EncryptionProvider.encryptString(adminUserUpdateReq.getPhone()));
                if (!adminUserUpdateReq.getPhone().equals(beforeRole.getPhoneEncrypt())) {
                    AdminUserDO user = userService.queryUserByPhone(adminUserUpdateReq.getPhone(), adminUserUpdateReq.getSysType());
                    if (user != null && !user.getId().equals(adminUserUpdateReq.getUserId())) {
                        return CommonResult.error(AdminUserResultCode.USER_PHONE_EXIST);
                    }
                }
            }
            if (StringUtils.hasText(adminUserUpdateReq.getEmail())) {
                adminUserUpdateReq.setEmail(aes256EncryptionProvider.encryptString(adminUserUpdateReq.getEmail()));
                if (!adminUserUpdateReq.getEmail().equals(beforeRole.getEmailEncrypt())) {
                    AdminUserDO user = userService.queryUserByEmail(adminUserUpdateReq.getEmail(), adminUserUpdateReq.getSysType());
                    if (user != null && !user.getId().equals(adminUserUpdateReq.getUserId())) {
                        return CommonResult.error(AdminUserResultCode.USER_EMAIL_EXIST);
                    }
                }
            }
            AdminUserDO adminUserDO = new AdminUserDO();
            if (StringUtils.hasText(adminUserUpdateReq.getPwd()) && StringUtils.hasText(adminUserUpdateReq.getPwdConfirm())) {
                if (!adminUserUpdateReq.getPwd().equals(adminUserUpdateReq.getPwdConfirm())) {
                    return CommonResult.error(AdminUserResultCode.SECOND_PASSWORD_IS_INCONSISTENT);
                }
                adminUserDO.setPwdEncrypt(aes256EncryptionProvider.encryptString(adminUserUpdateReq.getPwd()));
            }
            if (CollectionUtils.isNotEmpty(adminUserUpdateReq.getPartCodes())) {
                adminUserDO.setPartCode(JsonUtils.toJsonString(adminUserUpdateReq.getPartCodes()));
            }
            if (!StringUtils.hasText(adminUserUpdateReq.getUserName())) {
                adminUserUpdateReq.setUserName(beforeRole.getEmail());
            }
            BeanUtils.copyProperties(adminUserUpdateReq, adminUserDO);
            adminUserDO.setId(adminUserUpdateReq.getUserId());
            adminUserDO.setPhoneEncrypt(adminUserUpdateReq.getPhone());
            adminUserDO.setEmailEncrypt(adminUserUpdateReq.getEmail());
            // updateRole
            userService.updateUser(adminUserDO);
            //send msg
            // if (StringUtils.hasText(adminUserUpdateReq.getPwd()) && StringUtils.hasText(adminUserUpdateReq.getPwdConfirm())) {
            //     String templateCode = null;
            //     switch (CommonEnum.SYS_TYPE.from(beforeRole.getSysType())) {
            //         case AGENT:
            //             templateCode = smsTemplateProperties.getResetPwdAgentNotify();
            //             break;
            //         case MERCHANT:
            //             templateCode = smsTemplateProperties.getResetPwdMerchantNotify();
            //             break;
            //         default:
            //     }
            //     SmsSendReq smsSendReq = new SmsSendReq();
            //     smsSendReq.setAccount(adminUserDO.getUserName());
            //     smsSendReq.setSn(beforeRole.getSn());
            //     smsSendReq.setCode(adminUserUpdateReq.getPwd());
            //     smsSendReq.setTemplateCode(templateCode);
            //     smsSendReq.setPhone(adminUserUpdateReq.getPhone());
            //     commonAppService.sendSms(smsSendReq);
            // }

            // log
            AdminUserDto afterUser = new AdminUserDto();
            BeanUtils.copyProperties(beforeRole, afterUser);
            BeanUtils.copyProperties(adminUserUpdateReq, afterUser);
            businessChangeLog(beforeRole, afterUser);
            return CommonResult.success(null);
        }
        catch (Exception e) {
            log.error("updateUser error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> deleteUser(Long userId) {
        log.info("deleteUser userId:{}", userId);
        try {
            if (userId == null) {
                return CommonResult.error(CommonResultCode.PARAMETER_MISSING);
            }
            AdminUserDto beforeRole = userService.queryUserDto(userId);
            if (beforeRole == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            // updateRole
            userService.deleteUser(userId);
            // log
            AdminUserDto afterRole = new AdminUserDto();
            BeanUtils.copyProperties(beforeRole, afterRole);
            afterRole.setDeleteFlag(Boolean.TRUE);
            businessChangeLog(beforeRole, afterRole);
            return CommonResult.success(null);
        }
        catch (Exception e) {
            log.error("deleteUser error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> assignUserRole(AdminUserAddRoleReq adminUserAddRoleReq) {
        log.info("assignUserRole adminUserAddRoleReq:{}", JsonUtils.toJsonString(adminUserAddRoleReq));
        try {
            AdminUserDto beforeRole = userService.queryUserDto(adminUserAddRoleReq.getUserId());
            if (beforeRole == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            // updateRole
            userService.assignUserRole(adminUserAddRoleReq.getUserId(), adminUserAddRoleReq.getRoleCodes());
            // log
            AdminUserDto afterRole = new AdminUserDto();
            BeanUtils.copyProperties(beforeRole, afterRole);
            afterRole.setRoleCodes(adminUserAddRoleReq.getRoleCodes());
            // businessChangeLog(beforeRole, afterRole);
            return CommonResult.success(null);
        }
        catch (Exception e) {
            log.error("assignUserRole error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updatePwd(AdminUserUpdatePwdReq adminUserUpdatePwdReq) {
        log.info("updatePwd adminUserUpdatePwdReq:{}", JsonUtils.toJsonString(adminUserUpdatePwdReq));
        try {
            LocalAdminUserRequest localAdminUserRequest = (LocalAdminUserRequest) LocalContext.get();
            AdminUserDto beforeRole = userService.queryUserDto(localAdminUserRequest.getUserId());
            if (beforeRole == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            if (!adminUserUpdatePwdReq.getPwd().equals(adminUserUpdatePwdReq.getPwdConfirm())) {
                return CommonResult.error(AdminUserResultCode.SECOND_PASSWORD_IS_INCONSISTENT);
            }
            String oldPhone = aes256EncryptionProvider.decryptString(beforeRole.getPhoneEncrypt());
            // CommonResult<Void> voidCommonResult = authCodeService.checkAuthCode(oldPhone,
            // AdminUserEnum.AdminAuthCodeEnum.ADMIN_AUTH_CODE_UPDATE_PWD,
            // adminUserUpdatePwdReq.getSmsCode());
            // if (!voidCommonResult.isSuccess()) {
            // return CommonResult.error(CommonResultCode.AUTH_CODE_ERROR);
            // }
            String oldPwd = aes256EncryptionProvider.encryptString(adminUserUpdatePwdReq.getOldPwd());
            if (!oldPwd.equals(beforeRole.getPwdEncrypt())) {
                return CommonResult.error(AdminUserResultCode.USER_PWD_ERROR);
            }
            String passwordEncrypt = aes256EncryptionProvider.encryptString(adminUserUpdatePwdReq.getPwd());
            // updatePwd
            userService.updatePwd(localAdminUserRequest.getUserId(), passwordEncrypt, CommonEnum.YES_NO_TYPE.YES);
            // log
            AdminUserDto afterRole = new AdminUserDto();
            BeanUtils.copyProperties(beforeRole, afterRole);
            afterRole.setPwdEncrypt(passwordEncrypt);
            businessChangeLog(beforeRole, afterRole);
            return CommonResult.success(null);
        }
        catch (Exception e) {
            log.error("updatePwd error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateStatus(AdminUserUpdateStatusReq adminUserUpdateStatusReq) {
        log.info("updateStatus adminUserUpdateStatusReq:{}", JsonUtils.toJsonString(adminUserUpdateStatusReq));
        try {
            AdminUserDto beforeRole = userService.queryUserDto(adminUserUpdateStatusReq.getUserId());
            if (beforeRole == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            // updateRole
            userService.updateStatus(adminUserUpdateStatusReq.getUserId(), adminUserUpdateStatusReq.getStatus());
            // log
            AdminUserDto afterRole = new AdminUserDto();
            BeanUtils.copyProperties(beforeRole, afterRole);
            afterRole.setStatus(adminUserUpdateStatusReq.getStatus());
            businessChangeLog(beforeRole, afterRole);
            return CommonResult.success(null);
        }
        catch (Exception e) {
            log.error("updatePwd error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<String> resetPwd(Long userId) {
        log.info("resetPwd userId:{}", userId);
        String password = UserUtil.password();
        AdminUserDO adminUserDO = userService.queryUser(userId);
        if (adminUserDO == null) {
            return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
        }
        AdminUserDO snAdmin = userService.queryUserBySn(adminUserDO.getSn());
        if (snAdmin == null) {
            return CommonResult.error(AdminUserResultCode.ADMIN_USER_DOES_NOT_EXIST);
        }
        try {
            userService.updatePwd(snAdmin.getId(), password, CommonEnum.YES_NO_TYPE.NO);
        }
        catch (Exception e) {
            log.error("encrypt error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
        String templateCode = null;
        switch (CommonEnum.SYS_TYPE.from(snAdmin.getSysType())) {
            case AGENT:
                templateCode = smsTemplateProperties.getResetPwdAgentNotify();
                break;
            case MERCHANT:
                templateCode = smsTemplateProperties.getResetPwdMerchantNotify();
                break;
            default:
        }
        try {
            if (StringUtils.hasText(templateCode)) {
                SmsSendReq smsSendReq = new SmsSendReq();
                smsSendReq.setSn(snAdmin.getSn());
                smsSendReq.setAccount(snAdmin.getName());
                smsSendReq.setCode(password);
                smsSendReq.setPhone(aes256EncryptionProvider.decryptString(snAdmin.getPhoneEncrypt()));
                smsSendReq.setTemplateCode(templateCode);
                commonAppService.sendSms(smsSendReq);
            }
        }
        catch (Exception e) {
            log.error("user pwd forget send message fail:{}", e.getMessage(), e);
        }
        return CommonResult.success(password);
    }

    @Override
    public CommonResult<AdminUserResp> queryUserByAccount(String account) {
        AdminUserDto adminUserDto = null;
        try {
            if (!account.contains(StringPool.AT)) {
                adminUserDto = userService.queryUserByPhone(account, null);
                if (adminUserDto == null) {
                    adminUserDto = userService.queryUserByAccount(account);
                }
            }
            else {
                adminUserDto = userService.queryUserByEmail(account, null);
            }
            if (adminUserDto == null) {
                return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
            }
            // String language =
            // WebFrameworkUtils.getRequest().getHeader(LanguageEnum.LANGUAGE.getCode());
            AdminUserResp adminUserResp = UserDoToResp.INSTANCE.domainToResp(adminUserDto);
            // if (StringUtils.hasText(language) &&
            // !language.equals(adminUserDto.getLanguage())) {
            // userService.updateLanguage(adminUserDto.getId(),language);
            // adminUserResp.setLanguage(language);
            // }
            adminUserResp.setPwd(adminUserDto.getPwdEncrypt());
            return CommonResult.success(adminUserResp);
        }
        catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(CommonResultCode.DATA_ENCRYPT_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updatePhone(AdminUserPhoneUpdateReq adminUserPhoneUpdateReq) {
        log.info("updatePhone adminUserPhoneUpdateReq:{}", JsonUtils.toJsonString(adminUserPhoneUpdateReq));
        LocalAdminUserRequest localAdminUserRequest = (LocalAdminUserRequest) LocalContext.get();
        AdminUserDO beforeUser = userService.queryUser(localAdminUserRequest.getUserId());
        try {
            String oldPhone = aes256EncryptionProvider.decryptString(beforeUser.getPhoneEncrypt());
            CommonResult<Void> voidCommonResult = authCodeService.checkAuthCode(oldPhone, AdminUserEnum.AdminAuthCodeEnum.ADMIN_AUTH_CODE_UPDATE_PHONE,
                    adminUserPhoneUpdateReq.getSmsCode());
            if (!voidCommonResult.isSuccess()) {
                return CommonResult.error(CommonResultCode.AUTH_CODE_ERROR);
            }
            voidCommonResult = authCodeService.checkAuthCode(adminUserPhoneUpdateReq.getNewPhone(), AdminUserEnum.AdminAuthCodeEnum.ADMIN_AUTH_CODE_UPDATE_PHONE,
                    adminUserPhoneUpdateReq.getNewPhoneCode());
            if (!voidCommonResult.isSuccess()) {
                return CommonResult.error(CommonResultCode.AUTH_CODE_ERROR);
            }
            AdminUserDO updateAdminUserDO = new AdminUserDO();
            updateAdminUserDO.setId(beforeUser.getId());
            updateAdminUserDO.setPhoneEncrypt(aes256EncryptionProvider.encryptString(adminUserPhoneUpdateReq.getNewPhone()));
            userService.updateUser(updateAdminUserDO);
            // log
            AdminUserDto afterUser = new AdminUserDto();
            BeanUtils.copyProperties(beforeUser, afterUser);
            afterUser.setPhoneEncrypt(updateAdminUserDO.getPhoneEncrypt());
            businessChangeLog(beforeUser, afterUser);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        return CommonResult.success();
    }

    /**
     * log object
     * 
     * @param beforeUser
     * @param afterUser
     * @return
     */
    void businessChangeLog(AdminUserDO beforeUser, AdminUserDO afterUser) {
        businessLogService.addBusinessChangeLog(String.valueOf(beforeUser.getId()), AdminUserEnum.CHANGE_LOG_TYPE.USER.getCode(), CommonEnum.CHANGE_LOG_MODEL_TYPE.ADMIN,
                beforeUser, afterUser);
    }

}
